package br.com.tiespec.web.ManagedBean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;

import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;

import br.com.tiespec.ejb.EjbInterface.AprovacaoClientePortalInterface;
import br.com.tiespec.model.Enum.TipoSortOrder;
import br.com.tiespec.model.Modelo.ClientePortal;
import br.com.tiespec.model.Util.RNException;
import br.com.tiespec.web.Util.InitUtil;
import br.com.tiespec.web.Util.MsgUtil;
import br.com.tiespec.web.Util.WebUtil;

@Named("aprovacaoClientePortalBean")
@SessionScoped
public class AprovacaoClientePortalBean implements Serializable {

	private static final long serialVersionUID = 625022671722152148L;

	@EJB private AprovacaoClientePortalInterface ejb;
	
	private ClientePortal clientePortalInstance;
	private LazyDataModel<ClientePortal> clientePortalList;
	
	private String varNome;
	private String varUsuario;
	private Date varCadastroDe;
	private Date varCadastroAte;
	
	private String msgAprovacao;
	
	public String aprovacaoClientePortal(){
		init();
		return "/views/aprovacaoClientePortal/show.xhtml";
	}
	
	@PostConstruct
	public void init(){
		clientePortalInstance = new ClientePortal();
		clientePortalList = InitUtil.inicializaClientePortalList();
		varNome = null;
		varUsuario = null;
		varCadastroDe = null;
		varCadastroAte = null;
		filtrarTodosAprovacaoClientePortal();
	}
	
	@SuppressWarnings("serial")
	public void filtrarAprovacaoClientePortal(){
		clientePortalList = new LazyDataModel<ClientePortal>() {
			@Override
			public List<ClientePortal> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, String> filters) {
				WebUtil.setDataTableRows(pageSize);
				try {
					return ejb.listarClientePortal(first, pageSize, sortField, 
						TipoSortOrder.convertePrimeFaces(sortOrder.toString()), varNome, varUsuario, varCadastroDe, varCadastroAte);
				} catch (RNException e) {
					e.printStackTrace();
					return new ArrayList<ClientePortal>();
				}
			}
			@Override  
	        public ClientePortal getRowData(String rowKey) {  
	        	try {
		            return ejb.selecionarClientePortal(Long.parseLong(rowKey));
	        	} catch (RNException e) {
	        		e.printStackTrace();
	        		return new ClientePortal();
	        	} catch (NumberFormatException e){
	        		return new ClientePortal();
	        	}
	        }  
	        @Override  
	        public String getRowKey(ClientePortal objeto) {  
	        	if (objeto!=null)
	        		return ""+objeto.getId();
	        	return "";
	        }  
		};
		try {
			clientePortalList.setRowCount(ejb.countClientePortal(varNome, varUsuario, varCadastroDe, varCadastroAte));
		} catch (RNException e) {
			e.printStackTrace();
		}
		clientePortalList.setPageSize(5);
	}
	
	@SuppressWarnings("serial")
	public void filtrarTodosAprovacaoClientePortal(){
		clientePortalList = new LazyDataModel<ClientePortal>() {
			@Override
			public List<ClientePortal> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, String> filters) {
				WebUtil.setDataTableRows(pageSize);
				try {
					return ejb.listarClientePortal(first, pageSize, sortField, 
						TipoSortOrder.convertePrimeFaces(sortOrder.toString()), null, null, null, null);
				} catch (RNException e) {
					e.printStackTrace();
					return new ArrayList<ClientePortal>();
				}
			}
			@Override  
	        public ClientePortal getRowData(String rowKey) {  
	        	try {
		            return ejb.selecionarClientePortal(Long.parseLong(rowKey));
	        	} catch (RNException e) {
	        		e.printStackTrace();
	        		return new ClientePortal();
	        	} catch (NumberFormatException e){
	        		return new ClientePortal();
	        	}
	        }  
	        @Override  
	        public String getRowKey(ClientePortal objeto){  
	        	if (objeto!=null)
	        		return ""+objeto.getId();
	        	return "";
	        }  
		};
		try {
			clientePortalList.setRowCount(ejb.countClientePortal(null,null,null,null));
		} catch (RNException e) {
			e.printStackTrace();
		}
		clientePortalList.setPageSize(5);
	}
	
	public void goSelecionar(Long id){
		try {
			clientePortalInstance = ejb.selecionarClientePortalComResponsavel(id);
			msgAprovacao = MsgUtil.getMensagem("DialogoConfirmarAprovacaoCliente_Mensagem", clientePortalInstance.getNomeCompleto());
		} catch (RNException e) {
			clientePortalInstance = new ClientePortal();
		}
	}
	
	public void aprovarCliente(){
		try {
			ejb.aprovarClientePortal(clientePortalInstance.getId());
			MsgUtil.msgSucesso("apr", clientePortalInstance.getNomeCurto());
		} catch (RNException e){
			e.printStackTrace();
			MsgUtil.msgErro("apr", clientePortalInstance.getNomeCurto());
		}
	}
	
	public String goHome(){
		this.destroy();
		return "goHome";
	}
	
	@PreDestroy
	public void destroy(){
		clientePortalInstance = null;
		clientePortalList = null;
		varNome = null;
		varUsuario = null;
		varCadastroDe = null;
		varCadastroAte = null;
		msgAprovacao = null;
	}

	public ClientePortal getClientePortalInstance() {
		return clientePortalInstance;
	}

	public void setClientePortalInstance(ClientePortal clientePortalInstance) {
		this.clientePortalInstance = clientePortalInstance;
	}

	public LazyDataModel<ClientePortal> getClientePortalList() {
		return clientePortalList;
	}

	public void setClientePortalList(LazyDataModel<ClientePortal> clientePortalList) {
		this.clientePortalList = clientePortalList;
	}

	public String getVarNome() {
		return varNome;
	}

	public void setVarNome(String varNome) {
		this.varNome = varNome;
	}

	public String getVarUsuario() {
		return varUsuario;
	}

	public void setVarUsuario(String varUsuario) {
		this.varUsuario = varUsuario;
	}

	public Date getVarCadastroDe() {
		return varCadastroDe;
	}

	public void setVarCadastroDe(Date varCadastroDe) {
		this.varCadastroDe = varCadastroDe;
	}

	public Date getVarCadastroAte() {
		return varCadastroAte;
	}

	public void setVarCadastroAte(Date varCadastroAte) {
		this.varCadastroAte = varCadastroAte;
	}
	
	public String getMsgAprovacao() {
		return msgAprovacao;
	}
	
	public void setMsgAprovacao(String msgAprovacao) {
		this.msgAprovacao = msgAprovacao;
	}
}